/*!
 * \file frhash2_getLong.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

typedef struct st_test {
    char *p_data;
    char *p_value;
} test_st;

static void my_list_node_free(Frlist_node *node)
{
    if(((test_st *)node->data)->p_data)
        frfree(((test_st *)node->data)->p_data);

    frfree(((test_st *)node->data)->p_value);
    frfree((test_st *)node->data);
}

static void __case1(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    char *p_key = (char *)"test";
    unsigned int ui_key_len = strlen("test");

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_PTR_NULL(p_node);
}

static void __case2(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    char *p_key = (char *)"test";
    unsigned int ui_key_len = strlen("test");

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    p_hash_hdr->p_list_array = NULL;

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_PTR_NULL(p_node);
    frfree(p_hash_hdr);
}

static void __case3(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    char *p_key = (char *)"test";
    unsigned int ui_key_len = strlen("test");
    unsigned long ul_hash_size = 5;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_PTR_NULL(p_node);
    frhash2_free2(p_hash_hdr,my_list_node_free);
}

static void __case4(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    test_st *p_st_node = NULL;
    char *p_key = NULL;
    unsigned int ui_key_len = 0;
    unsigned long ul_hash_size = 5;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;

    p_st_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("test") + 1,"%s","test");

    p_st_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;
    p_st_node->p_data = (char *)frmalloc(strlen("case") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("case") + 1,"%s","case");

    p_st_node->p_value = (char *)frmalloc(strlen("case1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("case1") + 1,"%s","case1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //查找p_key
    ui_key_len = strlen("test");
    p_key = (char *)frmalloc(ui_key_len + 1);
    snprintf(p_key,ui_key_len + 1,"%s","test");

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_STRING_EQUAL(((test_st *)p_node->data)->p_value,"test1");
    frhash2_free2(p_hash_hdr,my_list_node_free);
    frfree(p_key);

}

static void __case5(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    test_st *p_st_node = NULL;
    char *p_key = NULL;
    unsigned int ui_key_len = 0;
    unsigned long ul_hash_size = 5;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;

    p_st_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("test") + 1,"%s","test");

    p_st_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;
    p_st_node->p_data = (char *)frmalloc(strlen("case") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("case") + 1,"%s","case");

    p_st_node->p_value = (char *)frmalloc(strlen("case1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("case1") + 1,"%s","case1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //查找p_key
    ui_key_len = 0;

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_PTR_NULL(p_node);

    frhash2_free2(p_hash_hdr,my_list_node_free);
}

static void __case6(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    test_st *p_st_node = NULL;
    char *p_key = NULL;
    unsigned int ui_key_len = 0;
    unsigned long ul_hash_size = 5;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;

    p_st_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("test") + 1,"%s","test");

    p_st_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;
    p_st_node->p_data = (char *)frmalloc(strlen("case") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("case") + 1,"%s","case");

    p_st_node->p_value = (char *)frmalloc(strlen("case1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("case1") + 1,"%s","case1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //查找p_key
    ui_key_len = strlen("case");
    p_key = (char *)frmalloc(ui_key_len + 1);
    snprintf(p_key,ui_key_len + 1,"%s","case");

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_STRING_EQUAL(((test_st *)p_node->data)->p_value,"case1");
    frhash2_free2(p_hash_hdr,my_list_node_free);
    frfree(p_key);

}

static void __case7(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    test_st *p_st_node = NULL;
    char *p_key = NULL;
    unsigned int ui_key_len = 0;
    unsigned long ul_hash_size = 5;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;

    p_st_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("test") + 1,"%s","test");

    p_st_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //查找p_key
    ui_key_len = strlen("test");
    p_key = (char *)frmalloc(ui_key_len + 1);
    snprintf(p_key,ui_key_len + 1,"%s","test");

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_STRING_EQUAL(((test_st *)p_node->data)->p_value,"test1");
    frhash2_free2(p_hash_hdr,my_list_node_free);
    frfree(p_key);

}

static void __case8(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_node = NULL;
    test_st *p_st_node = NULL;
    char *p_key = NULL;
    unsigned int ui_key_len = 0;
    unsigned long ul_hash_size = 5;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;

    p_st_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("test") + 1,"%s","test");

    p_st_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //建立struct并对结构体中的内容赋值
    p_st_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_st_node == NULL)
        return;
    p_st_node->p_data = (char *)frmalloc(strlen("case") + 1);
    if(p_st_node->p_data == NULL)
    {
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_data,strlen("case") + 1,"%s","case");

    p_st_node->p_value = (char *)frmalloc(strlen("case1") + 1);
    if(p_st_node->p_value == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node);
        return;
    }
    snprintf(p_st_node->p_value,strlen("case1") + 1,"%s","case1");

    //创建节点
    p_node = frlist_node_create(p_st_node);
    if(p_node == NULL)
    {
        frfree(p_st_node->p_data);
        frfree(p_st_node->p_value);
        frfree(p_st_node);
    }

    //加入hash表
    ui_key_len = strlen(p_st_node->p_data);
    frhash2_addStr(p_hash_hdr,p_st_node->p_data,ui_key_len,p_node);

    //查找p_key
    ui_key_len = strlen("name");
    p_key = (char *)frmalloc(ui_key_len + 1);
    snprintf(p_key,ui_key_len + 1,"%s","name");

    p_node = frhash2_getStr(p_hash_hdr, p_key, ui_key_len);

    CU_ASSERT_PTR_NULL(p_node);
    frhash2_free2(p_hash_hdr,my_list_node_free);
    frfree(p_key);

}

int add_suite_frhash2_getStr(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frhash2 frhash2_getStr()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);
    CU_add_test(pSuite, "case7", __case7);
    CU_add_test(pSuite, "case8", __case8);

    return FR_SUCC;
}
